package com.Luoyi.springframework.core.io;

import cn.hutool.core.lang.Assert;

import java.net.MalformedURLException;
import java.net.URL;

public class DefaultResourceLoader implements ResourceLoader{
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location,"加载路径必须不为空！");

        if (location.startsWith(CLASSPATH_URL_PREFIX)){
            //加载类路径资源
            return  new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        }else {
            try{
                //加载网络资源
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                //加载本地文件路径资源
                return new FileSystemResource(location);
            }
        }
    }
}
